﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace DevelopAssistant.Service.MSQLIntellisense
{
    public class IntellisenseBox : ListBox
    {
        private ImageList imageList;

        public ImageList ImageList
        {
            get
            {
                return this.imageList;
            }
            set
            {
                this.imageList = value;
                if (this.imageList == null)
                    return;
                this.ItemHeight = this.imageList.ImageSize.Height + 2;
                this.Height = this.ItemHeight * 10 + 2;
            }
        }

        public IntellisenseBox()
        {   
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.BorderStyle = BorderStyle.FixedSingle;
            this.Font = new Font("Arial", 9.0f);
        }        

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            Rectangle bounds = e.Bounds;
            Size imageSize = this.imageList.ImageSize;
            try
            {
                IntellisenseBoxItem intellisenseBoxItem = (IntellisenseBoxItem)this.Items[e.Index];
                intellisenseBoxItem.Bounds = bounds;
                if (intellisenseBoxItem.ImageIndex != -1)
                {                     
                    this.imageList.Draw(e.Graphics, bounds.Left, bounds.Top, intellisenseBoxItem.ImageIndex);
                }
                e.Graphics.DrawString(intellisenseBoxItem.Text, e.Font, (Brush)new SolidBrush(e.ForeColor), (float)(bounds.Left + imageSize.Width), (float)bounds.Top + 1);
            }
            catch(Exception ex)
            {
                if (e.Index != -1)
                    e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, (Brush)new SolidBrush(e.ForeColor), (float)bounds.Left, (float)bounds.Top + 1);
                else
                    e.Graphics.DrawString(this.Text, e.Font, (Brush)new SolidBrush(e.ForeColor), (float)bounds.Left, (float)bounds.Top + 1);
            }
            base.OnDrawItem(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);             
        }
    }
}
